<?php
/**
 * Created by PhpStorm.
 * User: 戎大富
 * Date: 2020/2/19
 * Time: 18:03
 */
namespace app\api\controller;
use think\Db;
class My extends Base {
    public function saveBiye()
    {
        header('Access-Control-Allow-Origin: *');
        if(isset($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] == 'OPTIONS'){
            exit();
        }
        $file = request()->file('file');
        $user_id = $_POST['user_id'];
        $dir = ROOT_PATH . 'public' . DS . 'uploads/images';
        if(!file_exists($dir)){
            //检查是否有该文件夹，如果没有就创建，并给予最高权限
            mkdir($dir, 0700,true);
        }
        $url =  http_type ();
        $info = $file->move($dir);
        if($info){
            $newName = $info->getSaveName();
            //压缩图片
            $this->image_png_size_add("$dir/{$newName}","$dir/{$newName}");
            $user = Db::table('up_u_user')->where('user_id',$user_id)->find();
            $biyepic = $this->addPicAndWenzi("$url/uploads/images/{$newName}",$user);
            $res = Db::table('up_u_user')->where('user_id',$user_id)->setField('user_biye_pic',$biyepic);
            if($res){
                return json_encode(['status'=>200,'msg'=>$biyepic]);
            }else{
                return json_encode(['status'=>100,'msg'=>'提交失败，请刷新重试~']);
            }

        }else{
            return json_encode(['status'=>100,'msg'=>$file->getError()]);
        }
    }
    public function makeUserPhoto()
    {
        header('Access-Control-Allow-Origin: *');
        if(isset($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] == 'OPTIONS'){
            exit();
        }
        $userChoosePic = $_POST['userChoosePic'];
        $user_id = $_POST['user_id'];
        $file = request()->file('file');
        $dir = ROOT_PATH . 'public' . DS . 'uploads/images';
        if(!file_exists($dir)){
            //检查是否有该文件夹，如果没有就创建，并给予最高权限
            mkdir($dir, 0700,true);
        }
        $url =  http_type ();
        $info = $file->move($dir);
        if($info){
            $newName = $info->getSaveName();
            //压缩图片
            $this->image_png_size_add("$dir/{$newName}","$dir/{$newName}");
//            return json(['status'=>200,'msg'=>"$url/{$newName}"]);
            $res = $this->getPepleFace("$url/uploads/images/{$newName}");
            if($res['code'] == 200){
                $two_to_one = $this->picToOne($userChoosePic,$url . $res['msg']);
                $endpic = $this->picLast($two_to_one);
                $res22 = Db::table('up_u_user')->where('user_id',$user_id)->setField('user_my_pic',$endpic);
                if($res22){
                    return json(['status'=>200,'msg'=>$endpic]);
                }else{
                    return json(['status'=>100,'msg'=>'生成失败，请刷新重试~']);
                }
            }else{
                return json(['status'=>100,'msg'=>'图片生成失败，请重试']);
            }

        }else{
            return json(['status'=>100,'msg'=>$file->getError()]);
        }
    }
}